<?php
namespace app\admin\controller;
use think\Db;
class Admin extends Base
{
    private $tn = 'admin';

    public function index()
    {
        if (isset($this->get['page']) && isset($this->get['limit'])) {
            $where = ['id' => ['>', 1]];
            if(session('admin_id') != 1){
                $where['id'] = session('admin_id');
            }
            if (isset($this->get['role'])) {
                $where['role'] = $this->get['role'];
            }
            $list = db($this->tn)->field(true)->where($where)->page($this->get['page'])->limit($this->get['limit'])->select();

            foreach ($list as $k => $v) {
                $list[$k]['role']        = get_field('admin_role', $v['role']);
                $list[$k]['create_time'] = date('Y-m-d H:i:s', $v['create_time']);
                if($v['area']){
                    $area = explode(',',$v['area']);
                    $area_name = Db::table('bz_area')->where('id','in',$area)->column('name');
                    $list[$k]['area'] = implode(',',$area_name);
                }
                
            }
            $count = db($this->tn)->where($where)->count();
            $this->returnAPI('', 0, $list, ['count' => $count, 'limit' => $this->get['limit']]);
        }
        return $this->fetch('', ['roles' => ['' => '用户角色'] + db('role')->column('name', 'id')]);
    }

    public function edit($id = 0)
    {
        $model = model($this->tn);
        if ($this->post) {
            if ($id) {
                $model = $model->get($id);
                if (is_null($model)) {
                    $this->returnAPI('信息有误，请稍后重试');
                }
            }
            $result = $model->validate($this->tn)->allowField(true)->save($this->post);
            if ($result !== false) {
                $this->returnAPI('操作成功', 0);
            }
            $this->returnAPI($model->getError());
        }
        $model = $model->get($id);
        if (is_null($model)) {
            $data = $this->table_fields($this->tn);
        } else {
            $data = $model->getData();
        }
        return $this->fetch('', [
            'data'  => $data,
            'roles' => ['' => '用户角色'] + db('role')->column('name', 'id'),
        ]);
    }

    //区域分配
    public function area($id=0){

        $model = model($this->tn);
        //获取已被使用的区域id
        $use_area = $model->where('id','neq',$id)->column('area');
        $use_area = array_unique(explode(',',implode(',',$use_area)));
        if ($this->post) {
            if(!isset($this->post['area']) || empty($this->post['area'])){
                $this->returnAPI('请选择要分配的区域');
            }
            //查看提交的区域是否有已经被用过的情况
            $area_diff = array_diff($use_area,$this->post['area']);
            if(count($area_diff) < count($use_area)){
                $this->returnAPI('分配权限存在已被分配的区域');
            }
          
            $model = $model->get($id);
            if (is_null($model)) {
                $this->returnAPI('信息有误，请稍后重试');
            }

            db()->startTrans();
            if($model['area'] != ''){
                $ids = explode(',',$model['area']);
                if(!Db::table('bz_area')->where('id','in',$ids)->update(['is_use'=>0])){
                    db()->rollback();
                    $this->returnAPI('修改区域状态失败1');
                }
            }

            if(!Db::table('bz_area')->where('id','in',$this->post['area'])->update(['is_use'=>1])){
                db()->rollback();
                $this->returnAPI('修改区域状态失败2');
            }
            $area = implode(',',$this->post['area']);
            if(!$model->save(['area'=>$area])){
                db()->rollback();
                $this->returnAPI('分配区域失败，请稍后再试');
            }
            db()->commit();
            $this->returnAPI('操作成功', 0);
            
        }

        $area = Db::table('bz_area')->where('arealevel',2)->where('id','notin',$use_area)->select();
        $pinyin = new \pinyin\ChineseToPinyin();
        $initial = [];
        foreach($area as $key=>$val){
            if($val['id'] == '500000'){
                $area[$key]['initial'] = 'C';
            }else{
                $area[$key]['initial'] = substr($pinyin->TransformUcwords($val['name']),0,1);
            }
            if(!in_array($area[$key]['initial'],$initial)){
                $initial[] = $area[$key]['initial'];
            }
        }
        sort($initial);
        $model = $model->get($id);
        if (is_null($model)) {
            $data = $this->table_fields($this->tn);
        } else {
            $data = $model->getData();
        }
        if($data['area'] == ''){
            $data['area'] = [];
        }else{
            $data['area'] = explode(',',$data['area']);
        }
       
        return $this->fetch('',['area'=>$area,'initial'=>$initial,'data'=>$data]);
    }

   
}
